package com.hebada.web.controller;

import com.google.common.collect.Maps;
import com.hebada.converter.CatalogConverter;
import com.hebada.converter.PhotoConverter;
import com.hebada.converter.ProductConverter;
import com.hebada.domain.Catalog;
import com.hebada.domain.Photo;
import com.hebada.domain.Product;
import com.hebada.entity.HttpMethod;
import com.hebada.entity.URLs;
import com.hebada.service.CatalogService;
import com.hebada.service.PhotoService;
import com.hebada.service.ProductService;
import com.hebada.utils.ImageUtils;
import com.hebada.web.vo.request.PageRequest;
import com.hebada.web.vo.request.PhotoListRequest;
import com.hebada.web.vo.request.PhotoRequest;
import com.hebada.web.vo.request.PhotoSearchRequest;
import com.hebada.web.vo.response.AjaxResponse;
import com.hebada.web.vo.response.PageResponse;
import com.hebada.web.vo.response.PhotoListResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.List;
import java.util.Map;

/**
 * Created by paddy on 2017/9/7.
 */
@RequestMapping(value = URLs.PHOTO)
@Api(value = "photo api", description = "photo api", basePath = URLs.PHOTO)
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
public class PhotoRestController {

    @Autowired
    private PhotoService photoService;
    @Autowired
    private CatalogService catalogService;
    @Autowired
    private ProductService productService;
    @Autowired
    private PhotoConverter photoConverter;
    @Autowired
    private CatalogConverter catalogConverter;
    @Autowired
    private ProductConverter productConverter;

    @RequestMapping(value = URLs.PHOTO_UPLOAD, method = RequestMethod.POST)
    @ApiOperation(value = "upload", notes = "image upload", httpMethod = HttpMethod.POST)
    public AjaxResponse upload(MultipartHttpServletRequest request) {
        Map<String, MultipartFile> fileMap = request.getFileMap();
        List<String> upload = ImageUtils.upload(fileMap, request.getServletContext().getRealPath(""));
        return AjaxResponse.ok().withData(upload);
    }

    @RequestMapping(value = URLs.PRODUCT_ID, method = RequestMethod.GET)
    @ApiOperation(value = "save", notes = "save image", httpMethod = HttpMethod.GET)
    public AjaxResponse get(@PathVariable long id) {
        return AjaxResponse.ok().withData(photoConverter.convertToPhotoResponse(photoService.get(id)));
    }

    @RequestMapping(value = URLs.DEFAULT, method = RequestMethod.POST)
    @ApiOperation(value = "save", notes = "save image", httpMethod = HttpMethod.POST)
    public AjaxResponse save(@Validated @RequestBody PhotoListRequest request) {
        photoService.save(photoConverter.convertToPhotoList(request));
        return AjaxResponse.ok();
    }

    @RequestMapping(value = URLs.PHOTO_ID, method = RequestMethod.DELETE)
    @ApiOperation(value = "delete", notes = "delete image", httpMethod = HttpMethod.DELETE)
    public AjaxResponse delete(@PathVariable long id) {
        photoService.delete(id);
        return AjaxResponse.ok();
    }

    @RequestMapping(value = URLs.PHOTO_ID, method = RequestMethod.PUT)
    @ApiOperation(value = "update", notes = "update image", httpMethod = HttpMethod.PUT)
    public AjaxResponse update(@PathVariable long id, @Validated @RequestBody PhotoRequest request) {
        photoService.update(id, photoConverter.convertToPhoto(request));
        return AjaxResponse.ok();
    }

    @RequestMapping(value = URLs.LIST, method = RequestMethod.POST)
    @ApiOperation(value = "photoList", notes = "photo list", httpMethod = HttpMethod.POST)
    public AjaxResponse getPhotoList(@RequestBody PageRequest<PhotoSearchRequest> request) {
        Page<Photo> photoPage;
        if (request.getParams() == null)
            photoPage = photoService.findPhotoListAll(request.getPageNumber(), request.getPageSize());
        else {
            Map<String, Object> params = Maps.newHashMap();
            params.put(PhotoSearchRequest.HAS_PRODUCT_PARAM, StringUtils.hasLength(request.getParams().getProductName()));
            params.put(PhotoSearchRequest.HAS_CATALOG_PARAM, StringUtils.hasLength(request.getParams().getCatalogName()));
            params.put(PhotoSearchRequest.PHOTO_NAME_KEY, request.getParams().getName());
            List<Catalog> catalogList = catalogService.findCatalogListByName(request.getParams().getCatalogName());
            List<Product> productList = productService.findProductListByName(request.getParams().getProductName());
            params.put(PhotoSearchRequest.CATALOG_KEY, catalogConverter.convertToCatalogIds(catalogList));
            params.put(PhotoSearchRequest.PRODUCT_KEY, productConverter.convertToProductIds(productList));
            photoPage = photoService.findPhotoList(params, request.getPageNumber(), request.getPageSize());
        }
        PageResponse<PhotoListResponse> response = new PageResponse<PhotoListResponse>(request.getPageSize(), request.getPageNumber(), photoPage.getTotalElements());
        if (photoPage.getTotalPages() == 0) return AjaxResponse.ok().withData(response);
        List<Catalog> catalogList = catalogService.findAllByIds(photoConverter.convertToCatalogIds(photoPage.getContent()));
        List<Product> productList = productService.findAllByProductIds(photoConverter.convertToProductIds(photoPage.getContent()));
        return AjaxResponse.ok().withData(photoConverter.convertToPhotoListResponses(response, photoPage.getContent(), catalogList, productList));
    }
}
